package jp.sinmetal.trainer.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityExistsException;

import jp.sinmetal.trainer.dto.ConventionMemberDto;
import jp.sinmetal.trainer.entity.ConventionMember;
import jp.sinmetal.trainer.exception.ConventionException;
import jp.sinmetal.trainer.util.DateUtil;

import org.seasar.struts.util.MessageResourcesUtil;

/**
 * 大会参加者サービス
 *
 * @author sin
 *
 */
public class ConventionMemberService extends AbstractService<ConventionMember> {

	/**
	 * 大会参加者IDを基にエンティティを返す
	 * @author sin
	 *
	 * @param conventionMemberId
	 * @return
	 */
	public ConventionMember findById(Integer conventionMemberId) {
		return select().id(conventionMemberId).getSingleResult();
	}

	/**
	 * 大会参加者一覧を返す
	 * @author sin
	 *
	 * @param conventionId
	 * @return
	 */
	public List<ConventionMemberDto> findByConventionId(Integer conventionId) {
		return jdbcManager
				.selectBySqlFile(ConventionMemberDto.class, getSqlFile("conventionMember/findByConventionId.sql"), conventionId)
				.getResultList();
	}

	/**
	 * 勝利数に達した大会参加者一覧を返す
	 * @author sin
	 *
	 * @param conventionId
	 * @param winCount
	 * @return
	 */
	public List<ConventionMemberDto> findEndWinCount(Integer conventionId, Integer winCount) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("conventionId", conventionId);
		paramMap.put("winCount", winCount);

		return jdbcManager
				.selectBySqlFile(ConventionMemberDto.class, getSqlFile("conventionMember/findEndWinCountMembers.sql"), paramMap)
				.getResultList();
	}

	/**
	 * 大会参加者数を返す
	 * @author sin
	 *
	 * @param conventionId
	 * @return
	 */
	public long getCount(Integer conventionId) {
		return select().where(SW().eq("conventionId", conventionId)).getCount();
	}

	/**
	 * フリーな大会参加者数を返す
	 * @author sin
	 *
	 * @param conventionId
	 * @return
	 */
	public long getFreeMemberCount(Integer conventionId) {
		return jdbcManager.selectBySqlFile(Long.class, getSqlFile("conventionMember/countFreeMembers.sql"), conventionId).getSingleResult();
	}


	/**
	 * フリーなメンバー一覧を返す
	 * @author sin
	 *
	 * @param conventionId
	 * @param order
	 * @return
	 */
	public List<ConventionMemberDto> findFreeMembers(Integer conventionId, String order) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("conventionId", conventionId);
		paramMap.put("order", order);

		return jdbcManager
				.selectBySqlFile(ConventionMemberDto.class, getSqlFile("conventionMember/findFreeMembers.sql"), paramMap)
				.getResultList();
    }

	/**
	 * 大会参加者作成
	 *
	 * @author sin
	 *
	 * @param entity
	 * @return
	 */
	public int insertConventionMember(ConventionMember entity) {
		entity.battleCount = 0;
		entity.winCount = 0;
		entity.activeFlag = true;
		entity.battleFlag = false;
		entity.acceptFlag = true;
		Date now = DateUtil.getNow();
//		entity.winDate = now;
		entity.entryDate = now;
		entity.updateDate = now;

		int result = 0;
		try {
			result = jdbcManager.insert(entity).execute();
		} catch(EntityExistsException ex) {
			String message = MessageResourcesUtil.getMessage("conventionMemberService.entityExists");
			throw new ConventionException(message);
		}
		return result;
	}

	/**
	 * 複数件更新
	 * @author sin
	 *
	 * @param entities
	 * @return
	 */
	public int[] updateBatch(List<ConventionMember> entities) {
		if (entities == null || entities.size() < 1) {
			return new int[0];
		}

		return jdbcManager.updateBatch(entities).execute();
	}

	/**
	 * アクティブフラグ更新
	 * @author sin
	 *
	 * @param entity
	 * @return
	 */
	public int update4ActiveFlag(ConventionMember entity) {
		return jdbcManager.update(entity).includes("activeFlag").execute();
	}
}